package com.lly.tboc.zhouyi

import android.net.Uri
import android.os.Parcelable
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavType
import androidx.navigation.navArgument
import com.lly.tboc.R
import kotlinx.parcelize.Parcelize


sealed class Screen(
    val route: String,
    @StringRes val resourceId: Int,
    @DrawableRes val iconId: Int,
    val enable: Boolean = true
) {

    object Audio : Screen("Audio/{uri}", R.string.player, R.drawable.qian) {
        val arguments: List<NamedNavArgument> = listOf(navArgument("uri") {
            type = NavType.StringType
        })

        fun route(data: Uri?): String {
            val uri = data.toString().let {
                Uri.encode(it)
            }
            return "Audio/$uri"
        }

        fun uri(backStackEntry: NavBackStackEntry): String {
            return backStackEntry.arguments?.getString("uri")?.let { Uri.decode(it) } ?: ""
        }

    }

    object Explore : Screen("explore", R.string.xuexi, R.drawable.qian)
    object Setting : Screen("setting", R.string.setting, R.drawable.qian)
    object Dao : Screen("dao", R.string.dao, R.drawable.taiji, enable = false)
    object Yao : Screen("yao/{guaId}", 0, 0) {
        val yaoRouteArguments: List<NamedNavArgument> = listOf(navArgument("guaId") {
            type = NavType.IntType
        })

        fun yaoRoute(guaId: Int) = "yao/${guaId}"
        fun guaId(backStackEntry: NavBackStackEntry, default: Int): Int {
            return backStackEntry.arguments?.getInt("guaId") ?: default
        }

    }

    object ZhouYi : Screen("gua/{guaId}", R.string.zhouyi, R.drawable.jiji) {
        val arguments: List<NamedNavArgument> = listOf(navArgument("guaId") {
            type = NavType.IntType
        })
        fun guaRoute(guaId: Int) = "gua/${guaId}"
        fun guaId(backStackEntry: NavBackStackEntry, default: Int): Int {
            return backStackEntry.arguments?.getInt("guaId") ?: default
        }
    }

    object Books : Screen("books", R.string.books, R.drawable.jiji)
    object WebView :
        Screen("webview", R.string.blank, R.drawable.jiji) {
        @Parcelize
        sealed class FileAddress(val path: String) : Parcelable {
            @Parcelize
            class Path(val p: String) : FileAddress(p)

            @Parcelize
            class Uri(val uri: android.net.Uri) : FileAddress(uri.toString())
        }
    }

    enum class FileType {
        Bitmap,
        Markdown,
        Dot,
        Txt,
        Svg,
        Audio,
        Video,
        UnKnow
    }

    fun String.suffix() = let {
        val start = it.indexOfLast { it == '.' }
        if (start < 0 || start >= it.length - 1) {
            return ""
        } else {
            it.substring(start + 1)
        }
    }

    fun String.isAssets() = this.startsWith("file:///android_asset/")
    fun String.isContent() = this.startsWith("content:/")
    fun String.startElli() = if (this.length > 30) "...${this.takeLast(30)}" else this

    object GuaEdit : Screen("guaEdit/{guaId}", 0, 0) {
        val guaEditRouteArguments: List<NamedNavArgument> = listOf(navArgument("guaId")
        {
            type = NavType.IntType
        })

        fun editRoute(guaId: Int) = "guaEdit/${guaId}"
        fun guaId(backStackEntry: NavBackStackEntry, default: Int): Int {
            return backStackEntry.arguments?.getInt("guaId") ?: default
        }
    }

    object CoroutineExplore : Screen(
        "coroutineExplore",
        R.string.coroutine_explore, R.drawable.taiji
    )

    object GuaSearch : Screen("search/gua", R.string.search, R.drawable.taiji) {

        fun searchRoute(keyword: String) = "search/gua"
        fun keyword(backStackEntry: NavBackStackEntry, default: String): String {
            return backStackEntry.arguments?.getString("keyword") ?: default
        }
    }
}

